﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Model;
using Besture.Bll;
using Besture.Common.Definition;
using Besture.Utils;
using Besture.Common;
using System.Text.RegularExpressions;

namespace Besture.UI.Land
{
    public partial class Registration : System.Web.UI.Page
    {
        //左图
        public ImageModel _BannerLeft { get; set; }
        public string _IsOnLine = AppConfig.IsOnLine;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
            GetBanner();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRegistration_Click(object sender, EventArgs e)
        {
            string newPw = registerPwd.Value.Trim();
            string newComPw = registerPwd.Value.Trim();
            string mobile = txtTelphone.Value.Trim();
            string nickName = txtNickName.Value.Trim();
            if (string.IsNullOrEmpty(nickName))
            {
                Fun.Alert(this.Page,"姓名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(mobile))
            {
                Fun.Alert(this.Page,"手机号不能为空");
                return;
            }
            if (!Regex.IsMatch(mobile, AppConfig.SMSRegex))
            {
                Fun.Alert(this.Page,"手机号码格式不正确");
                return;
            }
            //验证码
            if (registerCode.Value.ToUpper() != Session["SMSCode"] + "")
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "alertvalidation", "<script>$('#CardError').show();</script>");
                return;
            }
            Session["SMSCode"] = null;
            if (string.IsNullOrEmpty(newPw))
            {
                Fun.Alert(this.Page,"密码不能为空");
                return;
            }
            if (newPw != newComPw)
            {
                Fun.Alert(this.Page,"密码与确认密码不符");
                return;
            }

            UserDac dac = new UserDac();
            UserModel model = new UserModel();
            model.Phone = mobile;
            model.NickName = nickName;
            model.Password = Fun.GetMD5(newPw);
            model.CreateBy = 1;
            model.CreateTime = DateTime.Now;
            model.IsDeleted = 0;
            int flag = dac.Register(model);
            if (flag > 0)
            {
                model = dac.Login(mobile, Fun.GetMD5(newPw));
                Session[Constants.SESSION_KEY_LOGIN_USER] = model;
                Fun.Alert("注册成功", "/Index.aspx");
            }

        }

        #region 初始化数据
        private void InitData()
        {

        }
        #endregion

        #region 获取Banner
        /// <summary>
        /// 获取Banner
        /// </summary>
        public void GetBanner()
        {
            ImageDac imageDac = new ImageDac();
            ImageModel imgModel = imageDac.GetDataTableByCode(Constants.DICTIONARY_CODE_LandRegis, Constants.DICTIONARY_TYPE_Land);
            if (imgModel != null)
                _BannerLeft = imgModel;
            else
                _BannerLeft = new ImageModel();
        }
        #endregion
    }
}